export function setCookie(key, val, expires = null, path = "/") {
    if (expires == null || expires == undefined) {  // 不设置过期时间
        document.cookie = key + "=" + val + "; path=" + path;
    } else {
        var date = new Date();
        date.setSeconds(date.getSeconds() + expires);
        document.cookie = key + "=" + val + "; expires=" + date.toUTCString() + "; path=" + path;
    }
}

export function getCookie(key) {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; ")
        // console.log(list);
        for (var i = 0; i < list.length; i++) {
            var item = list[i];
            var k = item.split("=")[0];
            var v = item.split("=")[1];
            if (k === key) {
                return v;
            }
        }
    }
    return "";
}

export function delCookie(key) {
    setCookie(key, "", -1);
}

export function clearCookie() {
    var cookie = document.cookie;
    if (cookie) {
        var list = cookie.split("; ")
        // console.log(list);
        for (var i = 0; i < list.length; i++) {
            var item = list[i];
            var k = item.split("=")[0];
            delCookie(k);
        }
    }
}
